<!DOCTYPE html>
<html lang="zh">

<head>
	<meta charset="UTF-8">
	<meta content="width=device-width, initial-scale=1.0" name="viewport">
	<title>admin</title>
	<link href="https://cdn.staticfile.org/twitter-bootstrap/4.6.1/css/bootstrap.min.css" rel="stylesheet" />
	<link href="https://cdn.staticfile.org/bootswatch/4.6.1/darkly/bootstrap.min.css"
		media="(prefers-color-scheme: dark)" rel="stylesheet" />
	<style>
		body,
		input,
		textarea {
			font-family: "Microsoft JhengHei", "Meiryo", "Malgun Gothic", "Dotum", "MS PGothic", "PMingLiu", sans-serif;
		}

		body {
			background-color: #888;
		}

		@media (prefers-color-scheme: dark) {
			body {
				background: #222;
			}
		}
	</style>
</head>

<body>
	<script src="https://direct.r-mj.com/cache/apitest.js"></script>
	<script crossorigin="anonymous" src="https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js"></script>
	<script crossorigin="anonymous"
		src="https://cdn.staticfile.org/twitter-bootstrap/4.6.1/js/bootstrap.bundle.min.js"></script>
	<script>
		var webobj = {
			cid: 1,
			pw: "",
			json: {}
		};

		function save_table() {
			var tr = document.querySelectorAll("#table_data>tr")
			var text = "";
			for (var i = 0; i < tr.length; i++) {
				text += "\n" + (tr[i].innerText.trim().replace(/\s+/, "\t"));
			}
			document.getElementById("new_member_textarea").value = text.trim();
			addmember();
		}

		function delete_select() {
			var check_boxes = document.querySelectorAll('#table_data input:checked');
			var text = "";
			for (var i = 0; i < check_boxes.length; i++) {
				text += ' ' + check_boxes[i].parentNode.innerText;
			}
			document.getElementById("deltext").value = text.trim()
			document.getElementById("del_type").value = 2;
			del_log_or_member();
		}

		function now_time() {
			var now = new Date();
			return now.getFullYear() + "/"
				+ (now.getMonth() + 1) + "/"
				+ now.getDate() + " "
				+ now.getHours() + ":"
				+ now.getMinutes() + ":"
				+ now.getSeconds();
		}

		function display_post(json) {
			var now = now_time();
			if (json.hasOwnProperty("status") == false || json.hasOwnProperty("text") == false) {
				json = { "status": 2, "text": "空返回值#315" }
			}
			if (typeof json.text === "object") {
				json.text = JSON.stringify(json.text);
			}
			var div = document.createElement("div");
			var color = ["success", "warning", "danger", "info", "primary"];
			div.setAttribute("class", "alert alert-" + color[json.status] + " alert-dismissible fade show shadow-sm");
			div.setAttribute("role", "alert");
			var pre = document.createElement('pre');
			pre.appendChild(document.createTextNode(now + " " + json.text));
			div.appendChild(pre);
			var btn = document.createElement("button");
			btn.setAttribute("class", "close");
			btn.setAttribute("data-dismiss", "alert");
			btn.setAttribute("aria-label", "Close");
			var span = document.createElement("span");
			span.innerHTML = "&times;";
			btn.appendChild(span);
			div.appendChild(btn);
			var tgt = document.getElementById("return_msg")
			tgt.insertBefore(div, tgt.childNodes[0]);
			document.documentElement.scrollTop = 0;
		}

		(function () {
			var patt = new RegExp(/(.+)[?&]tdsourcetag=\w+$/);
			var arr = patt.exec(window.location.href);
			if (arr != null) {
				window.location.href = arr[1];
			}
			if (window.location.origin === "http://bot0.000.mk") {
				window.apiurl = "http://bot0.000.mk";
			} else {
				if (!window.hasOwnProperty("apiurl")) {
					window.apiurl = "https://cdn.r-mj.com";
				}
			}
			var querystr = [];
			if (querystr = document.location.hash.match(/\?cid=(\d+)&pw=(.+)/)) {
				document.location.hash = "";
				document.getElementById("login_cid").value = querystr[1];
				document.getElementById("login_passwd").value = querystr[2];
				login();
			}
		}())

		function bin2arr(bin, len) {
			//0b1101=>[1,0,1,1] 逆順序
			var arr = [];
			len = Math.abs(parseInt(len) || 0);
			while (len--) {
				arr.unshift((bin >> len) % 2);
			}
			return arr;
		}

		function show_config(json) {
			//display_post({status: 0, text: "登入成功"});
			webobj.json = json;
			$("#form1").addClass('d-none')
			$(".passed").removeClass('d-none');
			var input_txt = ["name", "pw",
				"area_name", "numerator", "qqgroup", "rule", "denominator", "admins", "address", "north", "east"];//id=i_XXXX
			var input_select = ["ctype", "status"];//i_type
			var input_checkbox = ["charge_by_hour", "no_smoking", "order_first", "scoreboard", "ban_gambling"];
			var setting_arr = bin2arr(json.setting, input_checkbox.length);
			var tgt = document.getElementById("table_data");
			tgt.textContent = '';
			var ta = "";
			var forarr = ["name", "qq", "gold", "balance"];
			for (var i = 0; i < json.members.length; i++) {
				var thisdata = json.members[i];
				ta += thisdata.phone + "\t"
					+ thisdata.name + "\t"
					+ thisdata.qq + "\t"
					+ thisdata.gold + "\t"
					+ thisdata.balance + "\r\n";
				var tmp_tr = document.createElement("tr");
				var td = document.createElement("td");
				var div = document.createElement("div");
				div.className = "form-check";
				var ipt = document.createElement("input");
				ipt.className = "form-check-input"
				ipt.setAttribute('type', "checkbox");
				ipt.setAttribute('id', "cb_" + thisdata.idx);
				var lable = document.createElement("label");
				lable.className = "form-check-label"
				lable.setAttribute('for', "cb_" + thisdata.idx);
				lable.innerText = thisdata.phone;
				div.appendChild(ipt);
				div.appendChild(lable);
				td.appendChild(div);
				tmp_tr.appendChild(td);
				for (var j = 0; j < forarr.length; j++) {
					var td = document.createElement("td");
					td.setAttribute('contenteditable', "true");
					td.innerText = thisdata[forarr[j]];
					tmp_tr.appendChild(td);
				}
				tgt.appendChild(tmp_tr);
			}
			$("#new_member_textarea").val(ta);
			for (var i = 0; i < input_txt.length; i++) {
				var tgt_id = input_txt[i];
				var e = document.getElementById("i_" + tgt_id);
				e.value = json[tgt_id];
			}
			for (var i = 0; i < input_select.length; i++) {
				var tgt_id = input_select[i];
				var e = document.getElementById("i_" + tgt_id);
				e.options[json[tgt_id]].selected = true;
			}
			for (var i = 0; i < input_checkbox.length; i++) {
				var tgt_id = input_checkbox[i];
				var e = document.getElementById(tgt_id);
				e.checked = setting_arr[i] == 1;
			}
		}

		function new_row() {
			var phone = parseInt(prompt("请输入手机号"));

			if (phone === NaN || phone < 10000000000) {
				return alert('手机号大于10000000000');
			}
			var user_name = prompt("请输入昵称").trim();
			if (user_name == '') {
				return alert('用户名不能是空');
			}
			if (!isNaN(user_name)) {
				return alert('用户名不能是纯数字');
			}
			var tr = document.createElement('tr');
			tr.innerHTML = '<td><div class="form-check"><label class="form-check-label" ><input class="form-check-input" type="checkbox">' + phone + '</label></div></td><td contenteditable="true">' + user_name + '</td><td contenteditable="true">0</td><td contenteditable="true">0</td><td contenteditable="true">0</td>';
			document.getElementById('table_data').appendChild(tr);
		}

		function addmember() {
			var postdata = {
				"cid": webobj.cid,
				"pw": webobj.pw,
				"q": "addmember",
				"order": $("#new_member_textarea").val()
			};
			return $.post(window.apiurl + "/r/addmember.php", postdata,
				function (data) {
					login();
					if (!data.hasOwnProperty("status") || !data.hasOwnProperty("text")) {
						return display_post({ status: 1, text: "返回空" });
					}
					return display_post(data);
				}, "json");
		}

		function addlog() {
			var postdata = {
				"cid": webobj.cid,
				"pw": webobj.pw,
				"q": "addlog",
				"is_national": document.getElementById("is_national").value,
				"order": $("#logtext").val()
			};
			return $.post(window.apiurl + "/r/addlog.php", postdata,
				function (data) {
					login();
					if (!data.hasOwnProperty("status") || !data.hasOwnProperty("text")) {
						return display_post({ status: 1, text: "返回空" });
					}
					return display_post(data);
				}, "json");
		}

		function before_post() {
			var newdata = $("form").serializeArray();
			newdata.push({ "name": "pw", "value": webobj.pw });
			newdata.push({ "name": "cid", "value": webobj.cid });
			newdata.push({ "name": "q", "value": "update_base" });
			var input_checkbox = ["charge_by_hour", "no_smoking", "order_first", "scoreboard", "ban_gambling"];
			var settings = "";
			for (var i = 0; i < input_checkbox.length; i++) {
				var e = document.getElementById(input_checkbox[i]);
				settings = (e.checked ? "1" : "0") + settings;
			}
			//console.log("s", settings);
			newdata.push({ "name": "setting", "value": parseInt(settings, 2) });

			//console.log(newdata);
			return $.post(window.apiurl + "/r/rate.php", newdata,
				function (data) {
					if (!data.hasOwnProperty("status") || !data.hasOwnProperty("text")) {
						return display_post({ status: 1, text: "返回空" });
					}
					return display_post(data);
				}, "json");
		}

		function del_log_or_member() {
			if (confirm('此操作很危险，要进行吗？') == false) {
				return;
			}
			var t = document.getElementById("del_type").value;
			if (t != 1 && t != 2 && t != 3) {
				return;
			}
			var type = ["none", "del_log", "del_member", "edit_phone"];
			var postdata = {
				"cid": webobj.cid,
				"pw": webobj.pw,
				"q": type[t],
				"order": $("#deltext").val()
			};
			return $.post(window.apiurl + "/r/del_log.php", postdata,
				function (data) {
					login();
					if (!data.hasOwnProperty("status") || !data.hasOwnProperty("text")) {
						return display_post({ status: 1, text: "返回空" });
					}
					return display_post(data);
				}, "json");
		}

		function login() {
			var cid = webobj.cid = document.getElementById("login_cid").value;
			var pw = webobj.pw = document.getElementById("login_passwd").value;
			$.post(window.apiurl + "/r/rate.php", {
				"q": "login",
				"cid": cid,
				"pw": pw
			}, function (data) {
				if (!data.hasOwnProperty("status")) {
					return display_post({ status: 1, text: "返回空" });
				}
				return (data.status == 0) ?
					show_config(data.data) : display_post(data);
			}, "json");
		}
	</script>

	<div class="container my-4">
		<div id="return_msg"></div>

		<div class="card shadow mb-3" id="form1">
			<div class="card-header text-center">登入</div>
			<div class="card-body">
				<div class="form-group">
					<label for="login_cid">雀庄编号</label>
					<input class="form-control" id="login_cid" placeholder="數字" type="number">
				</div>
				<div class="form-group">
					<label for="login_passwd">密碼</label>
					<input class="form-control" id="login_passwd" placeholder="密碼" type="password">
				</div>
			</div>
			<div class="card-footer">
				<input class="btn btn-primary btn-block" onclick="login()" type="button" value="登錄" />
			</div>
		</div>

		<form class="passed card shadow d-none" id="form2">
			<div class="card-header text-center">賽事設置</div>
			<div class="card-body">
				<h4>基本設置</h4>
				<div class="form-row">
					<div class="form-group col">
						<label for="i_name">雀庄名称</label>
						<input class="form-control" id="i_name" name="name" type="text" value="">
					</div>
					<div class="form-group col">
						<label for="i_pw">管理密碼</label>
						<input class="form-control" id="i_pw" name="new_pw" type="text" value="">
					</div>
				</div>
				<div class="collapse" id="area_tips">
					<div class="alert alert-info" role="alert">
						华北：北京/天津/河北/山西/内蒙古<br>
						东北：辽宁/吉林/黑龙江<br>
						华东：上海/江苏/浙江/安徽/福建/江西/山东/台湾<br>
						华中：河南/湖北/湖南<br>
						华南：广东/海南/广西/香港/澳门<br>
						西部：重庆/四川/贵州/云南/西藏/陕西/甘肃/青海/宁夏/新疆
					</div>
				</div>
				<div class="form-row">
					<div class="form-group col">
						<label for="i_ctype">地域
							<button aria-controls="area_tips" aria-expanded="false" class="btn btn-info btn-sm"
								data-target="#area_tips" data-toggle="collapse" type="button">
								说明
							</button>
						</label>


						<select class="custom-select mr-sm-2" id="i_ctype" name="ctype">
							<option value="0">其他</option>
							<option value="1">华北</option>
							<option value="2">东北</option>
							<option value="3">华东</option>
							<option value="4">中南</option>
							<option value="5">西部</option>
						</select>
					</div>
					<div class="col form-group">
						<label for="i_area_name">城市</label>
						<input class="form-control" id="i_area_name" name="area_name" placeholder="≤30字" type="text" />
					</div>
				</div>

				<div class="form-row mt-2">
					<div class="form-group col">
						<label for="i_status">雀庄狀態</label>
						<select class="custom-select mr-sm-2" id="i_status" name="status">
							<option value="0">不在首页显示</option>
							<option value="1">通常在周末营业</option>
							<option value="2">全天营业</option>
							<option value="3">暂时闭店</option>
							<option value="4">永久闭店</option>
						</select>
					</div>

					<div class="col form-group">
						<label for="i_qqgroup">雀庄QQ群</label>
						<input class="form-control" id="i_qqgroup" name="qqgroup" placeholder="數字" type="text" />
					</div>
				</div>
				<div class="form-group">
					<label for="i_address">雀庄地址</label>
					<input class="form-control" id="i_address" name="address" type="text" value="">
				</div>


				<div class="form-row">
					<div class="col">
						<label for="i_east">雀庄经度</label>
						<input class="form-control" id="i_east" name="east" type="text" value="" />
					</div>
					<div class="col">
						<label for="i_north">雀庄纬度</label>
						<input class="form-control" id="i_north" name="north" type="text" value="" />
					</div>
				</div>
				<div class="form-row mt-3">
					<div class="form-group col">
						<label for="i_admins">雀庄介绍</label>
						<textarea class="form-control" id="i_rule" name="rule" rows="5"></textarea>
						<small class="text-muted">
							※支持markdown語法
							<a href="https://www.jianshu.com/p/f3147a804368" target="_blank">具體看這</a>
						</small>
					</div>
					<div class="form-group col">
						<label for="i_admins">管理员QQ</label>
						<textarea class="form-control" id="i_admins" name="admins" rows="5"></textarea>
						<small class="text-muted">
							用空格或回车隔开
						</small>
					</div>
				</div>


				<div class="form-row mb-0">
					<div class="form-group col">
						<label for="i_numerator">活跃度,分子</label>
						<input class="form-control" id="i_numerator" name="numerator" type="text" value="" />

					</div>
					<div class="form-group col">
						<label for="i_denominator">活跃度,分母</label>
						<input class="form-control" id="i_denominator" name="denominator" type="text" value="" />
					</div>
				</div>
				<small class="text-muted">
					【活跃度】分子分母 输入 2/7 表示一星期，有2天雀庄营业。输入 15/30 表示一个月有15天营业。
				</small>
				<hr />
				<h4>雀庄状况</h4>

				<div class="custom-control custom-checkbox mb-3">
					<input class="custom-control-input" id="charge_by_hour" type="checkbox">
					<label class="custom-control-label" for="charge_by_hour">选中=按小时收费/非选中=按半庄收费</label>
				</div>

				<div class="form-row">
					<div class="form-group col">
						<div class="custom-control custom-checkbox">
							<input class="custom-control-input" id="no_smoking" type="checkbox">
							<label class="custom-control-label" for="no_smoking">禁止抽烟</label>
						</div>
					</div>

					<div class="form-group col">
						<div class="custom-control custom-checkbox">
							<input class="custom-control-input" id="order_first" type="checkbox">
							<label class="custom-control-label" for="order_first">必须事先预约</label>
						</div>
					</div>
				</div>

				<div class="form-row">
					<div class="form-group col">
						<div class="custom-control custom-checkbox">
							<input class="custom-control-input" id="scoreboard" type="checkbox">
							<label class="custom-control-label" for="scoreboard">有数显麻将机</label>
						</div>
					</div>

					<div class="form-group col">
						<div class="custom-control custom-checkbox">
							<input class="custom-control-input" id="ban_gambling" type="checkbox">
							<label class="custom-control-label" for="ban_gambling">严禁赌博</label>
						</div>
					</div>

				</div>
			</div>

			<div class="card-footer text-center">
				<input class="btn btn-primary btn-block" onclick="before_post()" type="button" value="提交" />
			</div>
		</form>

		<div class="passed card shadow mt-3 d-none" id="form3">
			<div class="card-body">
				<h4>多重记录导入
					<a href="https://docs.qq.com/doc/DZEVuYWFlQmR4em5v" target="_blank">说明</a>
				</h4>
				张三 李四 王五 赵六<br>
				25000 25000 25000 25000<br>
				320 321 322 37<br />
				<small class="text-muted">
					分数，可以缩小100倍或1000倍
				</small>
				<textarea class="form-control mb-3" id="logtext" name="logtext" rows="4"></textarea>
				<div class="input-group">
					<select class="custom-select" id="is_national">
						<option selected value="1">公式战</option>
						<!-- <option value="0">普通对局</option>-->
						<option value="0">普通对局(暂不开放)</option>
					</select>
					<div class="input-group-append">
						<button class="btn btn-primary" onclick="addlog()" type="button">多重记录导入</button>
					</div>
				</div>
			</div>
		</div>

		<div class="passed card shadow mt-3 d-none" id="form4">
			<div class="card-body">
				<h3>批量添加/修改会员
					<button class="btn btn-info btn-sm" data-target="#new_menber_tips" data-toggle="collapse"
						type="button">
						显示说明
					</button>
				</h3>

				<div class="collapse" id="new_menber_tips">
					<div class="alert alert-info" role="alert">
						如果手机号【不在】会员列表里，则会【添加新会员】<br>
						如果手机号【存在】会员列表里，则会【修改此人的信息】<br>
						可以批量添加，批量修改，或者两者混合<br>
						每行依次：手机号(必填) 昵称(必填) QQ号(选填) 积分(选填) 余额(选填)
						【例1 无QQ 积分100】12300001111 张三 = 100<br>
						【例2】12300001111 李四 123456789
					</div>
				</div>
				手机号 昵称 QQ号 积分 余额<br>
				<div class="form-group">
					<textarea class="form-control" id="new_member_textarea" name="new_member_textarea"
						rows="3"></textarea>
					<small class="text-muted">
						上方数据 与 电子表格/在线表格 可以互相粘贴
					</small>
				</div>
				<button class="btn btn-primary" onclick="addmember()" type="button">提交</button>
			</div>
		</div>

		<div class="passed card shadow mt-3 d-none" id="form5">
			<div class="card-body">
				<h4>批量删除记录+批量删除成员+批量改手机号</h4>
				<small class="text-muted">
					【批量删除成员】：輸入手機號
					【例】12344443333 12344446666<br>
					【批量删除记录】：輸入編號（記錄頁面#123字樣的東西）
					【例】122 123 22<br>
					【批量改手机号】：原手机号+新手机号 原手机号+新手机号
					【例】12344443333 12344446666 12344443333 12344446666<br>
					用【空格/回車】分隔，記得取消【不刪除】
				</small>
				<textarea class="form-control mb-3" id="deltext" rows="2"></textarea>
				<div class="input-group">
					<select class="custom-select" id="del_type">
						<option selected value="0">不刪除</option>
						<option value="1">刪除記錄</option>
						<option value="2">刪除成員</option>
						<option value="3">改手机号（测试中）</option>
					</select>
					<div class="input-group-append">
						<button class="btn btn-primary" onclick="del_log_or_member()" type="button">操作</button>
					</div>
				</div>
			</div>
		</div>

		<div class="passed card shadow mt-3 d-none" id="form6">
			<div class="card-body">
				<h3>修改会员信息</h3>
				<table class="table table-striped table-hover table-bordered table-sm mt-3 text-cent">
					<thead>
						<tr>
							<th scope="col">手机号</th>
							<th scope="col">昵称</th>
							<th scope="col">QQ</th>
							<th scope="col">积分</th>
							<th scope="col">余额</th>
						</tr>
					</thead>
					<tbody id="table_data">
						<tr>
							<td>
								<div class="form-check">
									<input class="form-check-input" id="flexCheckDefault" type="checkbox" value="">
									<label class="form-check-label" for="flexCheckDefault">12300001111</label>
								</div>
							</td>
							<td contenteditable="true">张三</td>
							<td contenteditable="true">123456789</td>
							<td contenteditable="true">0</td>
							<td contenteditable="true">0</td>
						</tr>
					</tbody>
				</table>
				<button class="btn btn-primary" onclick="new_row()" type="button">添加新成员</button>
				<button class="btn btn-danger" onclick="delete_select()" type="button">删除选中会员</button>
				<button class="btn btn-primary" onclick="save_table()" type="button">保存</button>
			</div>
		</div>
	</div>
</body>

</html>